package wili.b1.transform;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import wili.c.Ca;
import wili.c.File9a;

/**
 *
 */
class I18n_updateKey_fromGuid_toWord {

	public static void main(String[] args) {
		int opt = 1;
		opt = 2;
		if (opt == 1) {
			update();
		} else if (opt == 2) {

		}
	}

	static void update() {

		/*
		 * "label": "{i18n#accounting-tab-journal-remark}", "text":
		 * "{i18n#024a2d9c-f5f8-4ff2-9235-eaabd7b6100e/label}", "text":
		 * "{i18n>accounting}"
		 */
		Map<String, String> oldMap, newMap;
		File dir = new File("C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient");
		oldMap = File9a.mapOfFile(
				new File(dir,
						"java\\tcli\\tcli-resources\\src\\main\\resources\\detailView\\SalesDoc.detailView.properties"),
				"=");
		newMap = File9a.mapOfFile(new File(dir, "node\\client\\src\\content\\sales\\boODOC\\i18n\\i18n.properties"),
				"=");
		File jsonFile = new File("C:\\bearJ\\awilly\\src\\main\\java\\wili\\b1\\accountingTab.fragment.json");
		Set<String> newKeySet = new HashSet<>();
		StringBuilder sb = new StringBuilder();
		sb.append("\n#================weilai," + oldMap.size());
		String preKey = "acc_";
		Map<String, String> oldKeyToNewKeyMap = new HashMap<>();
		for (String oldKey : oldMap.keySet()) {
			String value = oldMap.get(oldKey);
			// Ca.log("oldKey=" + oldKey);
			if (oldKey.equals("accounting-tab-journal-remark"))
				Ca.pause();
			String newKey = preKey + value;
			newKey = newKey.replace("/label", "");
			{
				newKey = newKey.toLowerCase().replaceAll("\\s+|[^\\w]", "_").replaceAll("_+", "_").replaceFirst("_$",
						"");
			}
			oldKeyToNewKeyMap.put(oldKey, newKey);
			{//remove duplication.
				if (newMap.containsKey(newKey))
					continue;
				if (!newKeySet.add(newKey))
					continue;
			}
			sb.append("\n# XFLD, 64");
			sb.append("\n" + newKey + " = " + value);
		}
		Ca.log(1, sb.toString());
		{//update json string.
			String jsonSt = File9a.strOfFile(jsonFile, "");
			for (String oldKey : oldKeyToNewKeyMap.keySet()) {
				String newKey = oldKeyToNewKeyMap.get(oldKey);
				Ca.asert(!newKey.contains("\""), "");
				String st1 = r("`\\{i18n>" + oldKey + "(/label)?\\}`");
				String st2 = r("`{i18n>" + newKey + "}`");
				jsonSt = jsonSt.replaceFirst(st1, st2);

			}
			File9a.str2file(jsonSt, new File(jsonFile.getParent(), "t.json"), -1);
		}
	}

	static String r(String st) {
		return st.replace('`', '"');
	}

}
